//
//  GWBaseTableViewController.m
//  shandsHotel
//
//  Created by zh_s on 2018/9/25.
//  Copyright © 2018年 zh_s. All rights reserved.
//

#import "GWBaseTableViewController.h"
#import "GWHomeViewController.h"
#import "TestViewController.h"
#import "MiningVC.h"
#import "FindViewController.h"
#import "GWMineViewController.h"
#import <objc/runtime.h>

#import "GWCodeLoginViewController.h"

@interface GWBaseTableViewController ()<UITabBarControllerDelegate>
@property (strong, nonatomic) NSDate *lastDate;//先定义一个全局变量来记录上次点击的时间


@property (assign, nonatomic) BOOL isTest;
@end

@implementation GWBaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.customTabbar = [[CustomTabbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
//    self.customTabbar.backgroundColor = RandomColor;
    
    //使用运行时替换系统的
//    object_setClass(self.tabBar, [CustomTabbar class]);

    //去掉原生tabbar分割线
    [self.tabBar setShadowImage:[UIImage new]];
    [self.tabBar setBackgroundImage:[UIImage new]];

    //添加自定义customTabBar到系统tabBar上面
//    [self.tabBar addSubview:self.customTabbar];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeState) name:@"ChangeTabbarState" object:nil];;
    
    //添加tabbar背景颜色
//    UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -33, SCREEN_WIDTH, 130)];
    UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(-15, -33, SCREEN_WIDTH +30, CUSTOM_TABBAR_HEIGHT)];
    backView.image = IMAGE_NAME(@"Icon_Tab_Base_Image");
//    backView.contentMode = UIViewContentModeScaleAspectFit;
//    backView.clipsToBounds = YES;
    self.bgImage = backView;
    [self.tabBar insertSubview:backView atIndex:0];
    
//    self.tabBar.opaque = YES;
    self.delegate =self;
    
//    https://blog.csdn.net/longge_li/article/details/83654333
//    建议写个类别
    [UITabBar appearance].translucent = NO;

    
    [self setGeneralTabbarInfo];
    
}
- (void)addChildViewController:(UIViewController *)childController setImage:(NSString *)imageName setSelectedImage:(NSString *)selectedImage setTitle:(NSString *)title {
    
    childController.title = title;
    childController.tabBarItem.image = [UIImage imageNamed:imageName];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    UIColor *normalColor;
    UIColor *activeColor;
    if (_isTest) {
        normalColor = RandomColor;
        activeColor = RandomColor;
    }else {
//        normalColor = TEXT_COLOR_221;
        normalColor = RandomColor;
        activeColor = MAIN_ACTIVE_COLOR;
    }
    
//    //设置字体
//    NSMutableDictionary *normal = [NSMutableDictionary dictionary];
//    normal[NSForegroundColorAttributeName] = normalColor;
//    [childController.tabBarItem setTitleTextAttributes:normal forState:UIControlStateNormal];
//
//    NSMutableDictionary *selected = [NSMutableDictionary dictionary];
//    selected[NSForegroundColorAttributeName] = activeColor;
//    [childController.tabBarItem setTitleTextAttributes:selected forState:UIControlStateSelected];
    
    if (_isTest) {
        //未选中时文字颜色（正常颜色）
        [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateNormal];
        //选中时文字颜色（绿色）
        [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : RandomColor} forState:UIControlStateSelected];
    }else {
        //未选中时文字颜色（正常颜色）
        [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : TEXT_COLOR_221} forState:UIControlStateNormal];
        //选中时文字颜色（绿色）
        [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : MAIN_ACTIVE_COLOR} forState:UIControlStateSelected];
    }
    
  
    
    GWBaseNavigationController *nav = [[GWBaseNavigationController alloc] initWithRootViewController:childController];
    [childController.tabBarItem setImageInsets:UIEdgeInsetsMake(-5, 0, 0, 0)];

    [childController.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, 5)];

    if (![title isEqualToString:@"web"]) {
        [self addChildViewController:nav];
    }
    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
    
    
    NSInteger index = [tabBar.items indexOfObject:item];
    
    NSMutableArray *array = [NSMutableArray array];
    for (UIView *view in tabBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [array addObject:view];
        }
    }
    if (index != 1) {
        [self setGeneralTabbarInfo];
    }
    
    UIView *itemView = array[index];
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.5f;
    NSMutableArray *animationArray = [NSMutableArray array];
    [animationArray addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1)]];
    [animationArray addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1)]];
    [animationArray addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1)]];
    [animationArray addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1)]];
    animation.values = animationArray;
    
    [itemView.layer addAnimation:animation forKey:nil];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    GWBaseViewController *vc = tabBarController.selectedViewController;
    NSDate *date = [[NSDate alloc] init];
    if ([vc isEqual:viewController]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"doubleClick" object:nil];
        _lastDate = date;
        return NO;
    }
    
    return YES;
}

- (void)presendeCodeLogin {
    GWCodeLoginViewController *loginVC = [[GWCodeLoginViewController alloc] init];
    [GWUser writeIsPresentView:YES];//模态
    //    loginVC.isback = YES;
    // 包装一个导航栏控制器
    GWBaseNavigationController * nav = [[GWBaseNavigationController alloc] initWithRootViewController:loginVC];
    [self presentViewController:nav animated:YES completion:nil];
    
}

- (void)changeState {
    self.isTest = YES;
    [self setBlackTabbarInfo];
}

//常规
- (void)setGeneralTabbarInfo {
    self.isTest = NO;
    self.bgImage.image = IMAGE_NAME(@"Icon_Tab_Base_Image");
    [self addChildViewController:[[GWHomeViewController alloc] init] setImage:@"Icon_Tab Bar_Home_Inactive" setSelectedImage:@"Icon_Tab Bar_Home_Active" setTitle:@"首页"];
    
    [self addChildViewController:[[TestViewController alloc] init] setImage:@"Icon_Tab Bar_Test_Inactive" setSelectedImage:@"Icon_Tab Bar_Test_Active" setTitle:@"测试"];
    [self addChildViewController:[[FindViewController alloc] init] setImage:@"Icon_Tab Bar_Find_Inactive" setSelectedImage:@"Icon_Tab Bar_Find_Active" setTitle:@"发现"];
    
    [self addChildViewController:[MiningVC new] setImage:@"Icon_Tab Bar_User_Active" setSelectedImage:@"Icon_Tab Bar_User_Active" setTitle:@"我的"];
}
///黑色
- (void)setBlackTabbarInfo {
//    self.bgImage.image = IMAGE_NAME(@"Icon_Tab_Base_Image_block");
    
    [self addChildViewController:[[GWHomeViewController alloc] init] setImage:@"Icon_Tab Bar_Home_black" setSelectedImage:@"Icon_Tab Bar_Home_Active" setTitle:@"首页"];
    
    [self addChildViewController:[[TestViewController alloc] init] setImage:@"Icon_Tab Bar_Test_black" setSelectedImage:@"Icon_Tab Bar_Test_Active" setTitle:@"测试"];
    [self addChildViewController:[[FindViewController alloc] init] setImage:@"icon_mine_about" setSelectedImage:@"icon_mine_about" setTitle:@"发现"];
    
    [self addChildViewController:[[MiningVC alloc] init] setImage:@"Icon_Tab Bar_User_black" setSelectedImage:@"Icon_Tab Bar_User_black" setTitle:@"其他"];
}

- (void)setTitleBlackBackcolor {
//    UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
//    NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
//    dictNormal[NSForegroundColorAttributeName] = TEXT_COLOR_221;
//    dictNormal[NSFontAttributeName] = [UIFont systemFontOfSize:12];
//
//    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
//    dictSelected[NSForegroundColorAttributeName] = MAIN_ACTIVE_COLOR;
//    dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:12];
//
//    if (@available(iOS 10.0, *)) {
//        [[UITabBar appearance] setUnselectedItemTintColor:[UIColor whiteColor]];
//    } else {
//        // Fallback on earlier versions
//    }
//
//    [tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
//    [tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
